﻿Imports System
Imports System.IO
Imports System.Net.Mail

Public Class SendMail
    Implements ISendMail

    Private _MailAuthenticate As Boolean
    Private _MailEnabled As Boolean
    Private _MailPassword As String
    Private _MailPort As String
    Private _MailRecipients As String
    Private _MailServer As String
    Private _MailSSL As Boolean
    Private _MailUserName As String
    Private _ServiceEmail As String


    Public Property MailAuthenticate As Boolean Implements ISendMail.MailAuthenticate
        Get
            Return _MailAuthenticate
        End Get
        Set(ByVal value As Boolean)
            _MailAuthenticate = value
        End Set
    End Property

    Public Property MailEnabled As Boolean Implements ISendMail.MailEnabled
        Get
            Return _MailEnabled
        End Get
        Set(ByVal value As Boolean)
            _MailEnabled = value
        End Set
    End Property

    Public Property MailPassword As String Implements ISendMail.MailPassword
        Get
            Return "********"
        End Get
        Set(ByVal value As String)
            _MailPassword = value
        End Set
    End Property

    Public Property MailPort As String Implements ISendMail.MailPort
        Get
            Return _MailPort
        End Get
        Set(ByVal value As String)
            _MailPort = value
        End Set
    End Property

    Public Property MailRecipients As String Implements ISendMail.MailRecipients
        Get
            Return _MailRecipients
        End Get
        Set(ByVal value As String)
            _MailRecipients = value
        End Set
    End Property

    Public Property MailServer As String Implements ISendMail.MailServer
        Get
            Return _MailServer
        End Get
        Set(ByVal value As String)
            _MailServer = value
        End Set
    End Property

    Public Property MailSSL As Boolean Implements ISendMail.MailSSL
        Get
            Return _MailSSL
        End Get
        Set(ByVal value As Boolean)
            _MailSSL = value
        End Set
    End Property

    Public Property MailUserName As String Implements ISendMail.MailUserName
        Get
            Return _MailUserName
        End Get
        Set(ByVal value As String)
            _MailUserName = value
        End Set
    End Property

    Public Property ServiceEmail As String Implements ISendMail.ServiceEmail
        Get
            Return _ServiceEmail
        End Get
        Set(ByVal value As String)
            _ServiceEmail = value
        End Set
    End Property

    Public Sub New()
        _MailServer = My.Settings.MailServer
        _MailPort = My.Settings.MailPort
        _MailRecipients = My.Settings.MailRecipients
        _MailAuthenticate = My.Settings.MailAuthenticate
        _MailUserName = My.Settings.MailUserName
        _MailPassword = My.Settings.MailPassword
        _MailSSL = My.Settings.MailSSL
        _MailEnabled = My.Settings.MailEnabled
        _ServiceEmail = My.Settings.ServiceEmail
    End Sub

    Public Sub SendServiceMail(ByVal strSubject As String, ByVal strMessage As String) Implements ISendMail.SendServiceMail
        Try
            'Make sure that the mail setting is enabled.
            If MailEnabled Then

                'Create a SmtpClient object to handle the mail message
                Dim client As New SmtpClient(_MailServer)

                'Set the client port
                client.Port = _MailPort

                'Set the security
                client.EnableSsl = _MailSSL

                'Establish credentials
                If MailAuthenticate Then
                    Dim creds As New System.Net.NetworkCredential(_MailUserName, _MailPassword)
                    client.Credentials = creds
                End If

                'Create a MailMessage object
                Dim msg As New MailMessage(_ServiceEmail, _MailRecipients)

                'Set the message subject
                msg.Subject = strSubject

                'Set the message body
                msg.Body = strMessage

                'Send the message
                client.Send(msg)

                'Clean up
                client = Nothing
                msg = Nothing
            End If
        Catch ex As Exception
            System.Diagnostics.EventLog.WriteEntry("VOEIS Hub Push Service", "Error sending mail: " & Environment.NewLine & ex.Message.ToString & Environment.NewLine & "Check your mail settings via the service management client interface.", EventLogEntryType.Error)
        End Try
    End Sub
End Class
